package com.desksoft.service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import com.desksoft.common.BaseService;
import com.desksoft.dao.WebAddressDao;
import com.desksoft.entity.WebAddress;
import com.desksoft.vo.WebAddressVo;

/**
 * 文章service
 * @author forever
 *
 */
public class WebAddressService  extends BaseService<WebAddress>  {

	private WebAddressDao  webAddressDao;

	/**
	 * 查找公共的网站
	 * @return
	 */
	public List<WebAddress> findCommonWebAddress(){
		return webAddressDao.findCommonWebAddress();
	}
	
	public Integer insertWebAddress(WebAddress webAddress) {
		return webAddressDao.insertWebAddress(webAddress);
	}
	
	public Map<WebAddressVo, Map<WebAddressVo, List<WebAddressVo>>> findMainContent(){
        List<WebAddressVo> list  =  webAddressDao.findMainContent();
        //二级，三级的分类
        Map<String, List<WebAddressVo>> tempMap = new HashMap<String, List<WebAddressVo>>();
        Map<String, WebAddressVo> tempVo = new HashMap<String, WebAddressVo>();
        for(WebAddressVo vo : list){
        	tempVo.put(vo.getId(), vo);
        	if(1 == vo.getDepth()){
        		continue;
        	}
        	List<WebAddressVo> tempList = tempMap.get(vo.getFatherId());
        	if(tempList == null ){
        		tempList = new ArrayList<WebAddressVo>();
        	}
        	tempList.add(vo);
        	tempMap.put(vo.getFatherId(), tempList);
        }
        
//        Map<WebAddressVo, Map<WebAddressVo, List<WebAddressVo>>> map = new HashMap<WebAddressVo, Map<WebAddressVo,List<WebAddressVo>>>();
        Map<WebAddressVo, Map<WebAddressVo, List<WebAddressVo>>> map =  new TreeMap<WebAddressVo, Map<WebAddressVo,List<WebAddressVo>>>(
        		new Comparator<WebAddressVo>() {
					public int compare(WebAddressVo o1, WebAddressVo o2) {
						if(o1.getSequence().intValue() <=  o2.getSequence().intValue()){
							return -1 ;
						}
						return 1;
					}
        		});
        
//        Map<WebAddressVo, List<WebAddressVo>> tempDepth2Map = new HashMap<WebAddressVo, List<WebAddressVo>>();
//        for(WebAddressVo vo : list){
//        	if(2 == vo.getDepth()){
//        		List<WebAddressVo> d2list = tempMap.get(vo.getId());
//        		tempDepth2Map.put(vo, d2list);
//        	}
//        }
        
        for(WebAddressVo vo : list){
        	if (1 == vo.getDepth()){
        		String fatherId = vo.getId() ; 
        		Map<WebAddressVo, List<WebAddressVo>> childMap = new TreeMap<WebAddressVo, List<WebAddressVo>>(
                		new Comparator<WebAddressVo>() {
        					public int compare(WebAddressVo o1, WebAddressVo o2) {
        						if(o1.getSequence().intValue() <=  o2.getSequence().intValue()){
        							return -1 ;
        						}
        						return 1;
        					}
                });
        		List<WebAddressVo> childList = tempMap.get(vo.getId());
        		if(childList == null){
        			continue ;
        		}
        		for(WebAddressVo v : childList){
        			childMap.put(tempVo.get(v.getId()), tempMap.get(v.getId()));
        		}
        		
        		map.put(tempVo.get(fatherId), childMap);
        	}
        }
        
        for(WebAddressVo o : map.keySet()){
        	map.get(o);
        }
        return map ;
	}
	
	public List<WebAddress> findByParentId(String categoryId) {
		return webAddressDao.selectByParentId(categoryId);
	}

	public WebAddressDao getWebAddressDao() {
		return webAddressDao;
	}

	public void setWebAddressDao(WebAddressDao webAddressDao) {
		this.webAddressDao = webAddressDao;
	}

	@Override
	public Integer delete(String id) {
		return webAddressDao.delete(id);
	}

	@Override
	public Integer updateSequ(List<Map<String, Object>> listSequ) {
		Integer f = 0 ;
		for(Map<String, Object> map : listSequ){
			Integer temp = webAddressDao.updateSequ(map);
			if(temp == 1){
				f++;
			}
		}
		if(listSequ.size() == f){
			return 1 ;
		}
		return 0 ;
	}
}
